package edu.umn.cs5115.scheduler.search;


import java.util.Vector;
import java.util.ArrayList;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import edu.umn.cs5115.scheduler.*;
import edu.umn.cs5115.scheduler.entities.Course;
import edu.umn.cs5115.scheduler.entities.CourseFactory;
import edu.umn.cs5115.scheduler.framework.Application;

public class SearchDialog {
	
        /** Panel holding the search interface. */
        private Composite panel;
        
        /** Window controller this dialog is used by. */
        private SearchWindowController windowController;
    
	private Group grpBasicInfo;
	private Button btnAllDept;
	private Button btnSelectedDept;
	private Group grpDeptList;
	private List lstDept;
	private Combo cmbAddDept;
	private Button btnAddDept;
	private Button btnRemoveDept;
	private Button btnResetDept;
	private Text txtCourseNum;
	private Text txtCourseTitle;
	private Text txtInstructor;
	private Button btnDistanceOnly;
	private Button btnOpenSectionOnly;
	
	private Group grpTimeInfo;
	private Combo cmbTimeL;
	private Combo cmbTimeH;
	private Group grpDays;
	private Button btnMon;
	private Button btnTue;
	private Button btnWed;
	private Button btnThu;
	private Button btnFri;
	private Group grpSession;
	private List lstSession;
	
	private Group grpAdvOptions;
	private Combo cmbLevelL;
	private Combo cmbLevelH;
	private Combo cmbCreditL;
	private Combo cmbCreditH;
	private Group grpLiberalRqs;
	private List lstLiberalRqs;
	private Button btnNoRqs;
	private Group grpAttributes;
	private Button btnHonorOnly;
	private Button btnTeachingAwardOnly;
	private Button btnFullYearOnly;
	private Group grpLocations;
	private Button btnEastBank;
	private Button btnWestBank;
	private Button btnStPaul;
	private Button btnRochester;
	private Button btnOffTC;
	private Button btnOffR;
	
	private Button btnClose;
	private Button btnDefault;
	private Button btnSearch;
	
    private String [] DeptsFull;
    private String [] DeptsShort;
    private String [] Depts;
	
	public SearchDialog(SearchWindowController windowController)
    {
        this.windowController = windowController;
    }
    
    /**
     * Creates and adds the panel to the window controller's window.
     * @return The composite that was created and contains the search interface.
     */
    public Composite createPanel()
    {
        if (panel == null)
        {
            buildContent();
        }
        return panel;
    }
	
    public void buildContent()
    {
		WebInterface webInterface = WebInterface.getWebInterface();
    	
	    ArrayList<String> deptList = webInterface.getDepartments();
	    Depts = new String [deptList.size()];
	    DeptsShort = new String [deptList.size()];
	    DeptsFull = new String [deptList.size()];
	    
	    for (int i = 0; i < DeptsShort.length; ++i)
	    {
	    	int index1 = deptList.get(i).indexOf(" - ");
	    	int index2 = index1 + 3;
	    	DeptsFull[i] = deptList.get(i).substring(0, index1);
	    	DeptsShort[i] = deptList.get(i).substring(index2, deptList.get(i).length());
	    	Depts[i] = DeptsShort[i] + " - " + DeptsFull[i];
	    }
		
    	panel = new Composite(windowController.getWindow(), 0);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		panel.setLayout(gridLayout);
		GridData gridData;
		
		grpBasicInfo = new Group(panel, SWT.NULL);
		grpBasicInfo.setText("Basic Information");
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		grpBasicInfo.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL|GridData.VERTICAL_ALIGN_FILL);
		gridData.horizontalSpan = 2;
		grpBasicInfo.setLayoutData(gridData);
		
		new Label(grpBasicInfo, SWT.NULL).setText("Select Department:");
		
		btnAllDept = new Button(grpBasicInfo, SWT.RADIO);
		btnAllDept.setText("Show Courses Offered by Any Department");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalIndent = 5;
	    gridData.horizontalSpan = 2;
	    btnAllDept.setLayoutData(gridData);
	    btnAllDept.setSelection(true);
		btnAllDept.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				lstDept.setEnabled(false);
				cmbAddDept.setEnabled(false);
				btnAddDept.setEnabled(false);
				btnRemoveDept.setEnabled(false);
				btnResetDept.setEnabled(false);
			}
		});

	    btnSelectedDept = new Button(grpBasicInfo, SWT.RADIO);
		btnSelectedDept.setText("Only Show Courses from Departments in the List below");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalIndent = 5;
	    gridData.horizontalSpan = 2;
	    btnSelectedDept.setLayoutData(gridData);
	    btnSelectedDept.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				lstDept.setEnabled(true);
				cmbAddDept.setEnabled(true);
				cmbAddDept.setFocus();
				btnAddDept.setEnabled(true);
				if (lstDept.getSelectionCount() > 0)
					btnRemoveDept.setEnabled(true);
				btnResetDept.setEnabled(true);
			}
		});
	    
	    grpDeptList = new Group(grpBasicInfo, SWT.NULL);
		gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		grpDeptList.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 2;
		grpDeptList.setLayoutData(gridData);
		
	    lstDept = new List(grpDeptList, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
	    int listHeight = lstDept.getItemHeight() * 8;
	    Rectangle trim = lstDept.computeTrim(0, 0, 0, listHeight);
	    gridData.heightHint = trim.height;
	    gridData.horizontalSpan = 3;
	    gridData.grabExcessHorizontalSpace = true;
	    lstDept.setLayoutData(gridData);
	    lstDept.setEnabled(false);
	    lstDept.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				if (lstDept.getSelectionCount() > 0)
					btnRemoveDept.setEnabled(true);
				else
					btnRemoveDept.setEnabled(false);
			}
		});
	    
	    cmbAddDept = new Combo(grpDeptList, SWT.NONE);
	    cmbAddDept.setItems(Depts);
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalSpan = 3;
	    cmbAddDept.setLayoutData(gridData);
	    cmbAddDept.setVisibleItemCount(15);
	    cmbAddDept.select(0);
	    cmbAddDept.setEnabled(false);
	    cmbAddDept.addListener(SWT.KeyUp, new Listener () {
			public void handleEvent (Event event) {
				String current = cmbAddDept.getText();
				Boolean matched = false;
				for (int i = 0; i < DeptsShort.length; ++i)
				{
					if (current.equalsIgnoreCase(DeptsShort[i]) || (current.equalsIgnoreCase(DeptsFull[i])) || (current.equalsIgnoreCase(Depts[i])))
					{
						matched = true;
						break;
					}
				}
				btnAddDept.setEnabled(matched);
                                
                                // make it so that hitting enter adds a department
                                if (event.keyCode == 13 || event.keyCode == 3) // return and enter, respectively
                                {
                                    // fake a click on the add button
                                    btnAddDept.notifyListeners(SWT.Selection, new Event());
                                    return;
                                }
				if ((event.keyCode == 8) || (event.keyCode == 127))
					return;
				int length = cmbAddDept.getText().length();
				if (cmbAddDept.getSelection().equals(new Point(length, length)))
				{
					for (int i = 0; i < DeptsShort.length; ++i)
					{
						if (length >= DeptsShort[i].length())
							continue;
						if (current.equalsIgnoreCase(DeptsShort[i].substring(0,length)))
						{
							cmbAddDept.setText(DeptsShort[i]);
							cmbAddDept.setSelection(new Point(cmbAddDept.getText().length(), length));
							btnAddDept.setEnabled(true);
							return;
						}
					}
					for (int i = 0; i < DeptsFull.length; ++i)
					{
						if (length >= DeptsFull[i].length())
							continue;
						if (current.equalsIgnoreCase(DeptsFull[i].substring(0,length)))
						{
							cmbAddDept.setText(DeptsFull[i]);
							cmbAddDept.setSelection(new Point(cmbAddDept.getText().length(), length));
							btnAddDept.setEnabled(true);
							return;
						}
					}
					for (int i = 0; i < Depts.length; ++i)
					{
						if (length >= Depts[i].length())
							continue;
						if (current.equalsIgnoreCase(Depts[i].substring(0,length)))
						{
							cmbAddDept.setText(Depts[i]);
							cmbAddDept.setSelection(new Point(cmbAddDept.getText().length(), length));
							btnAddDept.setEnabled(true);
							return;
						}
					}
				}
			}
		});
	    
	    btnRemoveDept = new Button(grpDeptList, SWT.PUSH);
	    btnRemoveDept.setText("Remove");
	    btnRemoveDept.setEnabled(false);
	    btnRemoveDept.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				lstDept.remove(lstDept.getSelectionIndices());
				btnRemoveDept.setEnabled(false);
			}
		});
	    
	    btnResetDept = new Button(grpDeptList, SWT.PUSH);
	    btnResetDept.setText("Reset");
	    btnResetDept.setEnabled(false);
	    btnResetDept.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				lstDept.removeAll();
			}
		});
	    
	    btnAddDept = new Button(grpDeptList, SWT.PUSH);
	    btnAddDept.setText("Add Department to Search Criteria");
	    btnAddDept.setEnabled(false);
	    btnAddDept.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    btnAddDept.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				String current = cmbAddDept.getText();
				for (int i = 0; i < DeptsShort.length; ++i)
				{
					if (current.equalsIgnoreCase(DeptsShort[i]) || (current.equalsIgnoreCase(DeptsFull[i])) || (current.equalsIgnoreCase(DeptsShort[i] + " - " +DeptsFull[i])))
					{
						current = Depts[i];
						cmbAddDept.setText(current);
						break;
					}
				}
				for (String s : lstDept.getItems())
				{
					if (s.equalsIgnoreCase(current))
						return;
				}
				lstDept.add(current);
			}
		});
	    
	    Label label = new Label(grpBasicInfo, SWT.NULL);
	    label.setText("Course Number:");
	    label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    txtCourseNum = new Text(grpBasicInfo, SWT.SINGLE | SWT.BORDER);
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    txtCourseNum.setLayoutData(gridData);

	    label = new Label(grpBasicInfo, SWT.NULL);
	    label.setText("Course Title:");
	    label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    txtCourseTitle = new Text(grpBasicInfo, SWT.SINGLE | SWT.BORDER);
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    txtCourseTitle.setLayoutData(gridData);

	    label = new Label(grpBasicInfo, SWT.NULL);
	    label.setText("Instructor Last Name:");
	    label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    txtInstructor = new Text(grpBasicInfo, SWT.SINGLE | SWT.BORDER);
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    txtInstructor.setLayoutData(gridData);
	    
	    new Label(grpBasicInfo, SWT.NULL);
	    btnDistanceOnly = new Button(grpBasicInfo, SWT.CHECK);
	    btnDistanceOnly.setText("Show Only Independent and Distance Learning Courses");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalSpan = 2;
	    btnDistanceOnly.setLayoutData(gridData);
	    
	    btnOpenSectionOnly = new Button(grpBasicInfo, SWT.CHECK);
	    btnOpenSectionOnly.setText("Show Only Open Sections");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalSpan = 2;
	    btnOpenSectionOnly.setLayoutData(gridData);
	    btnOpenSectionOnly.setSelection(true);
	    
	    grpTimeInfo = new Group(panel, SWT.NULL);
		grpTimeInfo.setText("Time Information");
		gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		grpTimeInfo.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
		gridData.horizontalSpan = 1;
		grpTimeInfo.setLayoutData(gridData);
		
		new Label(grpTimeInfo, SWT.NULL).setText("Start Time After");
	    cmbTimeL = new Combo(grpTimeInfo, SWT.READ_ONLY);
	    cmbTimeL.setItems(new String [] {"8:00 AM", "9:00 AM", "10:00AM", "11:00AM", "12:00PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM"});
	    cmbTimeL.select(0);
	    cmbTimeL.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbTimeL.setVisibleItemCount(16);
	    cmbTimeL.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbTimeL.getSelectionIndex();
				if (cmbTimeH.getSelectionIndex() < index)
					cmbTimeH.select(index);
			}
		});
	    
	    new Label(grpTimeInfo, SWT.NULL).setText("End Time Before");
	    cmbTimeH = new Combo(grpTimeInfo, SWT.READ_ONLY);
	    cmbTimeH.setItems(new String [] {"9:00 AM", "10:00AM", "11:00AM", "12:00PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM", "11:59 PM"});
	    cmbTimeH.select(15);
	    cmbTimeH.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbTimeH.setVisibleItemCount(16);
	    cmbTimeH.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbTimeH.getSelectionIndex();
				if (cmbTimeL.getSelectionIndex() > index)
					cmbTimeL.select(index);
			}
		});
	    
	    grpDays = new Group(grpTimeInfo, SWT.NULL);
	    grpDays.setText("Days");
	    gridLayout = new GridLayout();
	    gridLayout.numColumns = 1;
	    grpDays.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
		gridData.horizontalSpan = 4;
		grpDays.setLayoutData(gridData);
		
		btnMon = new Button(grpDays, SWT.CHECK);
	    btnMon.setText("Monday");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnMon.setLayoutData(gridData);
		btnMon.setSelection(true);
		
		btnTue = new Button(grpDays, SWT.CHECK);
	    btnTue.setText("Tuesday");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnTue.setLayoutData(gridData);
		btnTue.setSelection(true);
		
		btnWed = new Button(grpDays, SWT.CHECK);
	    btnWed.setText("Wednesday");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnWed.setLayoutData(gridData);
		btnWed.setSelection(true);
		
		btnThu = new Button(grpDays, SWT.CHECK);
	    btnThu.setText("Thursday");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnThu.setSelection(true);
		
		btnFri = new Button(grpDays, SWT.CHECK);
	    btnFri.setText("Friday");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnFri.setLayoutData(gridData);
		btnFri.setSelection(true);
		
		grpSession = new Group(grpTimeInfo, SWT.NULL);
	    grpSession.setText("Session");
	    gridLayout = new GridLayout();
	    grpSession.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
		gridData.horizontalSpan = 4;
		gridData.grabExcessHorizontalSpace = true;
		grpSession.setLayoutData(gridData);
		
		lstSession = new List(grpSession, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
	    lstSession.setItems(new String [] {"All Sessions", "Regular Academic Session", "First Half of Term", "Second Half of Term"});
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
	    listHeight = lstSession.getItemHeight() * 4;
	    trim = lstSession.computeTrim(0, 0, 0, listHeight);
	    gridData.heightHint = trim.height;
	    gridData.grabExcessHorizontalSpace = true;
	    lstSession.setLayoutData(gridData);
	    lstSession.setSelection(0);
	    lstSession.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				if (lstSession.getSelectionCount() == 0)
					lstSession.setSelection(0);
			}
		});
            String controlKeyName = "Ctrl";
            if (Application.getOS() == Application.OS.MAC_OS_X)
                controlKeyName = "\u2318"; // don't modify the string, it works on Macs.
	    new Label(grpSession, SWT.NULL).setText(controlKeyName + " + Click to Multi-select");
	    
	    grpAdvOptions = new Group(panel, SWT.NULL);
		grpAdvOptions.setText("Advanced Options");
		gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		grpAdvOptions.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL|GridData.VERTICAL_ALIGN_FILL);
		gridData.horizontalSpan = 1;
		grpAdvOptions.setLayoutData(gridData);
		
		new Label(grpAdvOptions, SWT.NULL).setText("Level Range:");
	    cmbLevelL = new Combo(grpAdvOptions, SWT.READ_ONLY);
	    cmbLevelL.setItems(new String [] {"0xxx", "1xxx", "2xxx", "3xxx", "4xxx", "5xxx", "6xxx", "7xxx", "8xxx", "9xxx"});
	    cmbLevelL.select(0);
	    cmbLevelL.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbLevelL.setVisibleItemCount(10);
	    cmbLevelL.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbLevelL.getSelectionIndex();
				if (cmbLevelH.getSelectionIndex() < index)
					cmbLevelH.select(index);
			}
		});
	    
	    new Label(grpAdvOptions, SWT.NULL).setText("To");
	    cmbLevelH = new Combo(grpAdvOptions, SWT.READ_ONLY);
	    cmbLevelH.setItems(new String [] {"0xxx", "1xxx", "2xxx", "3xxx", "4xxx", "5xxx", "6xxx", "7xxx", "8xxx", "9xxx"});
	    cmbLevelH.select(9);
	    cmbLevelH.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbLevelH.setVisibleItemCount(10);
	    cmbLevelH.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbLevelH.getSelectionIndex();
				if (cmbLevelL.getSelectionIndex() > index)
					cmbLevelL.select(index);
			}
		});
	    
		new Label(grpAdvOptions, SWT.NULL).setText("Credit Range:");
	    cmbCreditL = new Combo(grpAdvOptions, SWT.READ_ONLY);
	    cmbCreditL.setItems(new String [] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9+"});
	    cmbCreditL.select(0);
	    cmbCreditL.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbCreditL.setVisibleItemCount(10);
	    cmbCreditL.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbCreditL.getSelectionIndex();
				if (cmbCreditH.getSelectionIndex() < index)
					cmbCreditH.select(index);
			}
		});
	    
	    new Label(grpAdvOptions, SWT.NULL).setText("To");
	    cmbCreditH = new Combo(grpAdvOptions, SWT.READ_ONLY);
	    cmbCreditH.setItems(new String [] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9+"});
	    cmbCreditH.select(9);
	    cmbCreditH.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
	    cmbCreditH.setVisibleItemCount(10);
	    cmbCreditH.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				int index = cmbCreditH.getSelectionIndex();
				if (cmbCreditL.getSelectionIndex() > index)
					cmbCreditL.select(index);
			}
		});
	    
	    grpLiberalRqs = new Group(grpAdvOptions, SWT.NULL);
	    grpLiberalRqs.setText("Liberal Eduation Requirements");
	    gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
	    grpLiberalRqs.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 4;
		grpLiberalRqs.setLayoutData(gridData);
	    
	    lstLiberalRqs = new List(grpLiberalRqs, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
	    lstLiberalRqs.setItems(new String [] {"Biological Science with Laboratory Core", "Citizenship and Public Ethics Theme", "Cultural Diversity Theme", "Environment Theme", "Historical Perspective Theme", "Literature Core", "Mathematical Thinking Core", "Other Humanities Core", "Physical Science with Laboratory Core", "Social Science Core", "Writing Intensive"});
	    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
	    listHeight = lstLiberalRqs.getItemHeight() * 6;
	    trim = lstLiberalRqs.computeTrim(0, 0, 0, listHeight);
	    gridData.heightHint = trim.height;
	    gridData.grabExcessHorizontalSpace = true;
	    gridData.horizontalSpan = 2;
	    lstLiberalRqs.setLayoutData(gridData);
	    
	    new Label(grpLiberalRqs, SWT.NULL).setText("Hold "+controlKeyName+" then Click to Multi-select");
	    btnNoRqs = new Button(grpLiberalRqs, SWT.PUSH);
	    btnNoRqs.setText("Reset");
		btnNoRqs.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    btnNoRqs.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				lstLiberalRqs.setSelection(1, 0);
			}
		});
	    
	    grpAttributes = new Group(grpAdvOptions, SWT.NULL);
	    grpAttributes.setText("Course Attributes");
	    gridLayout = new GridLayout();
	    grpAttributes.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 4;
		grpAttributes.setLayoutData(gridData);
		
	    btnHonorOnly = new Button(grpAttributes, SWT.CHECK);
	    btnHonorOnly.setText("Honor Courses Only");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnHonorOnly.setLayoutData(gridData);
	    
	    btnTeachingAwardOnly = new Button(grpAttributes, SWT.CHECK);
	    btnTeachingAwardOnly.setText("Teaching Award Winners Only");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnTeachingAwardOnly.setLayoutData(gridData);
	    
	    btnFullYearOnly = new Button(grpAttributes, SWT.CHECK);
	    btnFullYearOnly.setText("Full Year Enrollment Courses Only");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnFullYearOnly.setLayoutData(gridData);
	    
	    grpLocations = new Group(grpAdvOptions, SWT.NULL);
	    grpLocations.setText("Campus Areas");
	    gridLayout = new GridLayout();
	    gridLayout.numColumns = 3;
	    grpLocations.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 4;
		grpLocations.setLayoutData(gridData);
	    
		btnEastBank = new Button(grpLocations, SWT.CHECK);
	    btnEastBank.setText("East Bank");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnEastBank.setLayoutData(gridData);
	    btnEastBank.setSelection(true);
	    
	    btnWestBank = new Button(grpLocations, SWT.CHECK);
	    btnWestBank.setText("West Bank");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnWestBank.setLayoutData(gridData);
	    btnWestBank.setSelection(true);
	    
	    btnStPaul = new Button(grpLocations, SWT.CHECK);
	    btnStPaul.setText("St. Paul");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnStPaul.setLayoutData(gridData);
	    btnStPaul.setSelection(true);
	    
	    btnRochester = new Button(grpLocations, SWT.CHECK);
	    btnRochester.setText("Rocherster");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    btnRochester.setLayoutData(gridData);
	    btnRochester.setSelection(true);
	    
	    btnOffTC = new Button(grpLocations, SWT.CHECK);
	    btnOffTC.setText("Off Campus - Twin Cities");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalSpan = 2;
		btnOffTC.setLayoutData(gridData);
		btnOffTC.setSelection(true);
		
		btnOffR = new Button(grpLocations, SWT.CHECK);
	    btnOffR.setText("Off Campus - Rochester");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
	    gridData.horizontalSpan = 2;
		btnOffR.setLayoutData(gridData);
		btnOffR.setSelection(true);
		
		btnClose = new Button(panel, SWT.PUSH);
		btnClose.setText("Close");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnClose.setLayoutData(gridData);
                btnClose.addListener(SWT.Selection, new Listener()
                {
                    public void handleEvent(Event event)
                    {
                        btnClose.getShell().close();
                    }
                });
		
		btnDefault = new Button(panel, SWT.PUSH);
		btnDefault.setText("Restore to Default");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gridData.horizontalSpan = 2;
		btnDefault.setLayoutData(gridData);
	    btnDefault.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				btnAllDept.setSelection(true);
				btnSelectedDept.setSelection(false);
				lstDept.removeAll();
				lstDept.setEnabled(false);
				cmbAddDept.select(0);
				cmbAddDept.setEnabled(false);
				btnAddDept.setEnabled(false);
				btnRemoveDept.setEnabled(false);
				btnResetDept.setEnabled(false);
				txtCourseNum.setText("");
				txtCourseTitle.setText("");
				txtInstructor.setText("");
				btnDistanceOnly.setSelection(false);
				btnOpenSectionOnly.setSelection(true);
				cmbTimeL.select(0);
				cmbTimeH.select(15);
				btnMon.setSelection(true);
				btnTue.setSelection(true);
				btnWed.setSelection(true);
				btnThu.setSelection(true);
				btnFri.setSelection(true);
				lstSession.setSelection(0);
				cmbLevelL.select(0);
				cmbLevelH.select(9);
				cmbCreditL.select(0);
				cmbCreditH.select(9);
				lstLiberalRqs.setSelection(1, 0);
				btnHonorOnly.setSelection(false);
				btnTeachingAwardOnly.setSelection(false);
				btnFullYearOnly.setSelection(false);
				btnEastBank.setSelection(true);
				btnWestBank.setSelection(true);
				btnStPaul.setSelection(true);
				btnRochester.setSelection(true);
				btnOffTC.setSelection(true);
				btnOffR.setSelection(true);
			}
		});
		
		btnSearch = new Button(panel, SWT.PUSH);
		btnSearch.setText("Search");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		btnSearch.setLayoutData(gridData);
	    btnSearch.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event event) {
				//TODO : Splash screen indicating progress
				
				SchedulerDocument document = (SchedulerDocument) windowController.getDocument();
				
				//test whether the search is a blank one
				boolean empty = true;
				if (lstDept.getItemCount() > 0)
					empty = false;
				if (!txtCourseNum.getText().equals(""))
					empty = false;
				if (!txtCourseTitle.getText().equals(""))
					empty = false;
				if (!txtInstructor.getText().equals(""))
					empty = false;
				if (btnDistanceOnly.getSelection() == true)
					empty = false;
				if (cmbTimeL.getSelectionIndex() != 0)
					empty = false;
				if (cmbTimeH.getSelectionIndex() != 15)
					empty = false;
				if (btnMon.getSelection() == false)
					empty = false;
				if (btnTue.getSelection() == false)
					empty = false;
				if (btnWed.getSelection() == false)
					empty = false;
				if (btnThu.getSelection() == false)
					empty = false;
				if (btnFri.getSelection() == false)
					empty = false;
				if (lstSession.getFocusIndex() != 0)
					empty = false;
				if (cmbLevelL.getSelectionIndex() != 0)
					empty = false;
				if (cmbLevelH.getSelectionIndex() != 9)
					empty = false;
				if (cmbCreditL.getSelectionIndex() != 0)
					empty = false;
				if (cmbCreditH.getSelectionIndex() != 9)
					empty = false;
				if (lstLiberalRqs.getSelectionCount() > 0)
					empty = false;
				if (btnHonorOnly.getSelection() == true)
					empty = false;
				if (btnTeachingAwardOnly.getSelection() == true)
					empty = false;
				if (btnFullYearOnly.getSelection() == true)
					empty = false;
				if (btnEastBank.getSelection() == false)
					empty = false;
				if (btnWestBank.getSelection() == false)
					empty = false;
				if (btnStPaul.getSelection() == false)
					empty = false;
				if (btnRochester.getSelection() == false)
					empty = false;
				if (btnOffTC.getSelection() == false)
					empty = false;
				if (btnOffR.getSelection() == false)
					empty = false;
				if (empty == true)
				{
					document.displayDocumentError("Please specify your search criteria before pushing this button.");
					return;
				}
				
				//build search query
				SearchQuery query = new SearchQuery();
				WebInterface newpage = WebInterface.getWebInterface();
				query.searchTerm(newpage.getSessions().get(2));
				if (lstDept.isEnabled())
				{
					for(int i = 0; i < lstDept.getItemCount(); ++i)
					{
						for(String s : DeptsShort)
						{
							if (!lstDept.getItem(i).startsWith(s))
								continue;
							query.addSubject(lstDept.getItem(i).substring(0, lstDept.getItem(i).indexOf("-")-1));
							break;
						}
					}
				}
				if (!txtCourseNum.getText().equals(""))
				{
					query.searchCourseNumber(txtCourseNum.getText());
				}
				if (!txtCourseTitle.getText().equals(""))
				{
					query.searchCourseTitle(txtCourseTitle.getText());
				}
				if (!txtInstructor.getText().equals(""))
				{
					query.searchInstructorName(txtInstructor.getText());
				}
				query.searchIDLOnly(btnDistanceOnly.getSelection());
				query.searchOpenSections(btnOpenSectionOnly.getSelection());
				query.searchStartTime(ConvertTime(cmbTimeL.getText()));
				query.searchEndTime(ConvertTime(cmbTimeH.getText()));
				query.searchDay("Mon", btnMon.getSelection());
				query.searchDay("Tue", btnTue.getSelection());
				query.searchDay("Wed", btnWed.getSelection());
				query.searchDay("Thu", btnThu.getSelection());
				query.searchDay("Fri", btnFri.getSelection());
				for (String S : lstSession.getSelection())
				{
					query.addSession(S);
				}
				query.searchLowerLevelLimit(cmbLevelL.getText());
				query.searchUpperLevelLimit(cmbLevelH.getText());
				query.searchLowerCreditLimit(cmbCreditL.getText());
				query.searchUpperCreditLimit(cmbCreditH.getText());
				for (String Rq : lstLiberalRqs.getSelection())
				{
					query.addLibEdReq(Rq);
				}
				query.searchHonorsOnly(btnHonorOnly.getSelection());
				query.searchTeachingAwardOnly(btnTeachingAwardOnly.getSelection());
				query.searchFullYearOnly(btnFullYearOnly.getSelection());
				if (btnEastBank.getSelection() ==  true)
				{
					query.addCampusArea("East Bank");
				}
				if (btnWestBank.getSelection() ==  true)
				{
					query.addCampusArea("West Bank");
				}
				if (btnStPaul.getSelection() ==  true)
				{
					query.addCampusArea("St. Paul");
				}
				if (btnRochester.getSelection() ==  true)
				{
					query.addCampusArea("Rochester");
				}
				if (btnOffTC.getSelection() ==  true)
				{
					query.addCampusArea("Off Campus - TC");
				}
				if (btnOffR.getSelection() ==  true)
				{
					query.addCampusArea("Off Campus - Rochester");
				}
				SearchResults results;
                //TODO - erase when parsing works, terrible hack for testing
				
				/*
				java.util.List<Course> ghettoResults = new Vector<Course>();
				
				
				
				
				if(lstLiberalRqs.getSelectionCount() == 1 && lstLiberalRqs.getItem(lstLiberalRqs.getSelectionIndex()).equals("Literature Core"))
				{
	    			for(Course course : CourseFactory.getAllCourses(document))
	    			{
	    				System.out.println(course.getCourseNumber());
	    				if(course.getCourseNumber().equals("3461") || course.getCourseNumber().equals("1117W"))
	    					ghettoResults.add(course);
	    			}
	    			results = new SearchResults(ghettoResults, document);
				}
				else
					results = new SearchResults(newpage.search(query, document), document);
				*/
				results = new SearchResults(query, document);
                windowController.showSearchResults(results, true);
                //TODO: Kill splash screen now that it has finished processing
			}
		});
		
		panel.pack ();
    }
    
    private static String ConvertTime(String input)
    {
    	if (input.equals("8:00 AM")) return "8:00";
    	if (input.equals("9:00 AM")) return "9:00";
    	if (input.equals("10:00 AM")) return "10:00";
    	if (input.equals("11:00 AM")) return "11:00";
    	if (input.equals("12:00 PM")) return "12:00";
    	if (input.equals("1:00 PM")) return "13:00";
    	if (input.equals("2:00 PM")) return "14:00";
    	if (input.equals("3:00 PM")) return "15:00";
    	if (input.equals("4:00 PM")) return "16:00";
    	if (input.equals("5:00 PM")) return "17:00";
    	if (input.equals("6:00 PM")) return "18:00";
    	if (input.equals("7:00 PM")) return "19:00";
    	if (input.equals("8:00 PM")) return "20:00";
    	if (input.equals("9:00 PM")) return "21:00";
    	if (input.equals("10:00 PM")) return "22:00";
    	if (input.equals("11:00 PM")) return "23:00";
    	if (input.equals("11:59 PM")) return "23:59";
    	return "";
    }
}
